extern crate serde_json;

use self::serde_json::{json, Value};
use super::{do_cache, do_cache_ret, do_node, do_node_ret};
use base::base::{VBase, VMap};

#[allow(dead_code)]
pub async fn ck_psn(p: String, val: Value) {
    let cmd = vec!["task", "psn1", "psn2", "psn3"];

    let val = val.kobj().unwrap();

    for d in cmd.iter() {
        //println!("===> {d}");

        let m = psn_cmd(d);

        match val.k_val(d) {
            Some(v) => {
                do_node_ret(p.clone(), m.clone(), json!(v)).await;
                do_cache_ret(p.clone(), m, json!(v)).await;
            }
            None => {
                do_node(p.clone(), m.clone()).await;
                do_cache(p.clone(), m).await;
            }
        }
    }
}

#[allow(dead_code)]
pub fn psn_cmd(cmd: &str) -> Value {
    match cmd {
        "task" => json!({"line": "1", "tp": "task", "sub": "psn", "sn": "task1"}),
        "psn1" | "psn2" | "psn3" => json!({"line": "1", "tp": "psn", "sn": cmd}),
        _ => panic!("psn_cmd"),
    }
}
